package com.masterclient.app.businessmodule.minemodule.store;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.widget.empty.FCPageStateView;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.common.config.BusinessConfig;
import com.eoner.managerlibrary.router.AroutePath;
import com.eoner.middlelib.router.map.FCRouterPath;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.minemodule.store.bean.CollectShopBean;
import com.masterclient.app.businessmodule.minemodule.store.contract.ShopListContract;
import com.masterclient.app.businessmodule.minemodule.store.contract.ShopListPresenter;
import com.masterclient.app.modules.search.SearchShopMessage;
import com.masterclient.app.modules.search.adapter.SearchShopRecyclerAdapter;

import butterknife.BindView;

/**
 * @auther hjian
 * @date 2020/4/15.
 */
@Route(path = FCRouterPath.SELLER_DETAIL_PATH)
public class StoreFavoriteActivity extends FCBusinessActivity<ShopListPresenter> implements ShopListContract.View {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private int page,total;
    private SearchShopRecyclerAdapter favoriteAdapter;
    private View footView;
    private FCPageStateView emptyView;

    @Override
    protected boolean useRefreshLayout() {
        return true;
    }

    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_store_favorite;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setTitle("关注店铺").setLineHidden(true);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {

        total = 0;
        page = 1;
        baseRefreshLayout().setBackgroundResource(R.color.mainbg);
        recyclerView.setBackgroundResource(R.color.mainbg);
        recyclerView.setPadding(0, ScreenUtils.dp2px(10),0,0);
        recyclerView.setClipToPadding(false);

        favoriteAdapter = new SearchShopRecyclerAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(favoriteAdapter);
        favoriteAdapter.bindToRecyclerView(recyclerView);

        emptyView = new FCPageStateView(this);
        emptyView.setViewState(FCPageViewState.PAGE_STATE_NO_DATA,R.mipmap.null_img,"您还没有关注任何店铺");
        emptyView.setVisibility(View.GONE);
        footView = View.inflate(this, R.layout.view_foot_text, null);
        footView.setVisibility(View.GONE);
        favoriteAdapter.setEmptyView(emptyView);
        favoriteAdapter.setFooterView(footView);

        baseRefreshLayout().setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mPresenter.getCollectShopList(page+"", BusinessConfig.PAGE_SIZE,true);
            }
        });

        favoriteAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getCollectShopList(page+"",BusinessConfig.PAGE_SIZE,false);
            }
        },recyclerView);

        favoriteAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SearchShopMessage bean = favoriteAdapter.getData().get(position);
                ARouter.getInstance().build(AroutePath.STORE_ACTIVITY).withString("store_id",bean.getSh_id()).navigation();

            }
        });

        mPresenter.getCollectShopList(page+"",BusinessConfig.PAGE_SIZE,false);
    }


    @Override
    public void showShopList(CollectShopBean data) {
        baseEndRefresh();

        total = Integer.parseInt(data.getSh_total());
        if (page  == 1){
            if (data == null || data.getSh_shops().size() <= 0) {
                mPresenter.lists.clear();
                emptyView.setVisibility(View.VISIBLE);
            } else {
                emptyView.setVisibility(View.GONE);
                mPresenter.lists = data.getSh_shops();
            }
        }else {
            mPresenter.lists.addAll(data.getSh_shops());
        }

        favoriteAdapter.setNewData(mPresenter.lists);
        ++page;

        if (mPresenter.lists.size()>=total||data.getSh_shops().size()<Integer.parseInt(BusinessConfig.PAGE_SIZE)) {
            favoriteAdapter.setEnableLoadMore(false);
            footView.setVisibility(View.VISIBLE);
        } else {
            footView.setVisibility(View.GONE);
        }

    }

    @Override
    public void baseShowError() {
        super.baseShowError();
        baseEndRefresh();
        emptyView.setVisibility(View.GONE);
        if (page > 1) {
            favoriteAdapter.loadMoreFail();
        }
    }

    @Override
    public void onClickPageStateErrorView() {
        super.onClickPageStateErrorView();
        mPresenter.getCollectShopList(page+"",BusinessConfig.PAGE_SIZE,false);
    }

}
